#' ---
#' title: "Whose Dimension is it anyway: SRS Legislator and Citzins Scaling"
#' author: "Lukas F. Stoetzer"
#' ---

#' Script estimates irt models for SRS Study

  # Libraries
  library(girt)

  # Set values for mcmc
  Niter <- 40000
  Nburin <- 10000
  Nthin <- 10
  Nchains <- 4

  # Start Time
  start.time <- Sys.time()
  
# 1) SRS ================
  
  # Script
  cat("\n Estimaing Model for SRS \n")
  
  # Load SRS Study (Downloaded from Dataverse https://doi.org/10.7910/DVN/IIYGGX)
  load("Formatted+info+votes+matrices.RData")
  load("Survey+Responses+Dataframe.Rdata")
  
  # Legislator Data
  ld <- VOTES.SEN.SMALL
  
  # Respondent Data
  rd <- VOTES.RESP.SMALL
  
  # Covariates
  polinfo <- apply(INFO,1,mean,na.rm=T)
  polinfo <- (polinfo-mean(polinfo))/sd(polinfo)
  partisan <- ifelse(survey.wd$pid7 %in% c(1,2,6,7),1,0)
  partisan[is.na(survey.wd$pid7)] <- NA
  democrat <- ifelse(survey.wd$pid7 %in% c(1,2),1,0)
  republican <- ifelse(survey.wd$pid7 %in% c(6,7),1,0)
  democrat[is.na(survey.wd$pid7)] <- NA
  republican[is.na(survey.wd$pid7)] <- NA
  
  # Summary table
  stargazer::stargazer(data.frame(rd, polinfo, democrat, republican))
  
  
  # 1.1) Direct Effects ================
  
  # model Matrix
  Z <- as.matrix(cbind(1, # Intercept
                       partisan,
                       polinfo))
  
  # listwise delition for respondents
  Z <- na.omit(Z)
  desl <- na.action(Z) 
  
  # Model
  res_SRS_m1 <- MCMCirtG(Yl = ld,Yc =  rd[-desl,], X =     Z,
                         burnin=Nburin,gibbs = Niter,thin = Nthin,
                         chains=Nchains,
                         prior_var_gamma = 100, proposal_var = 0.009)
  
  # Save Data
  save(res_SRS_m1,file="SRS_irt_bayes_m1.Rdata")
  rm(res_SRS_m1)
  
  # 1.2) Interaction Effects ================
  
  # Data Matrix
  Z <- as.matrix(cbind(1, # Intercept
                       partisan,
                       polinfo,
                       partisan*polinfo))
  
  # listwise delition for respondents
  Z <- na.omit(Z)
  desl <- na.action(Z) 
  
  res_SRS2_m2 <- MCMCirtG(Yl = ld,Yc = rd[-desl,], X = Z,
                          burnin=Nburin,gibbs = Niter,thin = Nthin,
                          chains=Nchains,
                          prior_var_gamma = 100, proposal_var = 0.009)
  
  # Save Data
  save(res_SRS2_m2,file="SRS_irt_bayes_m2.Rdata")  
  rm(res_SRS2_m2)
  
  

  
# 1.4) Democrat Republican Interaction Effects ================
  
  # Data Matrix
  Z <- as.matrix(cbind(1, # Intercept
                       democrat,republican,
                       polinfo,
                       democrat*polinfo,republican*polinfo))
  
  # listwise delition for respondents
  Z <- na.omit(Z)
  desl <- na.action(Z) 
  
  res_SRS2_m4 <- MCMCirtG(Yl = ld,Yc = rd[-desl,], X = Z,
                          burnin=Nburin,gibbs = Niter,thin = Nthin,
                          chains=Nchains,
                          prior_var_gamma = 100, proposal_var = 0.009)
  
  # Save Data
  save(res_SRS2_m4,file="SRS_irt_bayes_m4.Rdata")      
  rm(res_SRS2_m4)
  
  
  # Time Track
  end.time <- Sys.time()
  time.taken <- end.time - start.time
  print(time.taken)